//------------------------------------------------------------------------------
#ifndef LOCALSERVER_H
#define LOCALSERVER_H
//------------------------------------------------------------------------------
#include "clientconnection.h"
#include "stnmessage.h"

#include <QLocalServer>
#include <QSet>
//------------------------------------------------------------------------------
/*!
 * Default name for the local server (named pipe)
 */
#define DEFAULT_SERVER_NAME	"SystrayNotifier.LocalSocket"
//------------------------------------------------------------------------------
/*!
 * Local server, listens on socket and receives message from clients
 * Received messages emits "messageReceived" signal
 */
class LocalServer : public QObject {
	Q_OBJECT
//------------------------------------------------------------------------------
	public:
		/*!
		 * Constructor, initializes local server
		 * @param serverName Name of the local server (name of the named pipe)
		 */
		LocalServer(const QString &serverName=DEFAULT_SERVER_NAME, QObject *parent=0);

		/*!
		 * Destructor
		 */
		~LocalServer();

		/*!
		 * Asks the local server to start listening
		 * @return True if the server has started listening successfully
		 */
		bool start();
//------------------------------------------------------------------------------
	private slots:
		/*!
		 * The local server received a new client connection
		 */
		void newConnection_received();

		/*!
		 * Client disconnected from the server
		 */
		void client_disconnected();
//------------------------------------------------------------------------------
	signals:
		/*!
		 * The server received a message from a client
		 */
		void messageReceived(STNMessage m);
//------------------------------------------------------------------------------
	private:
		/*!
		 * Local server (named pipe)
		 */
		QLocalServer *m_server;

		/*!
		 * Server name (name of the pipe)
		 */
		QString m_serverName;

		/*!
		 * Set of connected clients
		 */
		QSet<ClientConnection *> m_clientConnections;
//------------------------------------------------------------------------------
};
//------------------------------------------------------------------------------
#endif // LOCALSERVER_H
//------------------------------------------------------------------------------
